<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="账期年月日" prop="billDay">
        <el-input
          v-model="queryParams.billDay"
          placeholder="请输入账期年月日"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

<!--    <el-row :gutter="10" class="mb8">-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['accounting:batch:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['accounting:batch:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['accounting:batch:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['accounting:batch:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
<!--    </el-row>-->

    <el-table v-loading="loading" :data="batchList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键 自增" align="center" prop="id" />-->
      <el-table-column label="对账批次" align="center" prop="batchNo" />
      <el-table-column label="账期" align="center" prop="billDay" />
      <el-table-column label="对账日期" align="center" prop="checkDay" />
<!--      <el-table-column label="同一天内的批次序号(对账年月日内)" align="center" prop="batchNum" />-->

      <el-table-column label="渠道名称" align="center" prop="billType" />
<!--      <el-table-column label="平台支付订单笔数" align="center" prop="orderCount" />-->
<!--      <el-table-column label="拉卡拉账单笔数" align="center" prop="checkCount" />-->
      <el-table-column label="差错账数量" align="center" prop="diffCount" />
      <el-table-column label="是否完成对账" align="center" prop="completeState" />
      <el-table-column label="对账确认时间" align="center" prop="confirmDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.confirmDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="生成区分" align="center" prop="createType" />
      <el-table-column label="账单下载批次" align="center" prop="downloadBatch" />
      <el-table-column label="备注" align="center" prop="remarks" />
<!--      <el-table-column label="预留字段1" align="center" prop="reserve1" />-->
<!--      <el-table-column label="预留字段2" align="center" prop="reserve2" />-->
<!--      <el-table-column label="预留字段3" align="center" prop="reserve3" />-->
<!--      <el-table-column label="预留字段4" align="center" prop="reserve4" />-->
<!--      <el-table-column label="预留字段5" align="center" prop="reserve5" />-->
<!--      <el-table-column label="删除标识,0:未删除，1：删除" align="center" prop="delFlg" />-->
<!--      <el-table-column label="创建用户ID" align="center" prop="createUserid" />-->
<!--      <el-table-column label="更新用户ID" align="center" prop="updateUserid" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['accounting:batch:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['accounting:batch:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改对账批次对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="账期年月日" prop="billDay">
          <el-input v-model="form.billDay" placeholder="请输入账期年月日" />
        </el-form-item>
        <el-form-item label="对账年月日(yyyymmdd)" prop="checkDay">
          <el-input v-model="form.checkDay" placeholder="请输入对账年月日(yyyymmdd)" />
        </el-form-item>
        <el-form-item label="同一天内的批次序号(对账年月日内)" prop="batchNum">
          <el-input v-model="form.batchNum" placeholder="请输入同一天内的批次序号(对账年月日内)" />
        </el-form-item>
        <el-form-item label="批次号" prop="batchNo">
          <el-input v-model="form.batchNo" placeholder="请输入批次号" />
        </el-form-item>
        <el-form-item label="平台支付订单笔数" prop="orderCount">
          <el-input v-model="form.orderCount" placeholder="请输入平台支付订单笔数" />
        </el-form-item>
        <el-form-item label="拉卡拉账单笔数" prop="checkCount">
          <el-input v-model="form.checkCount" placeholder="请输入拉卡拉账单笔数" />
        </el-form-item>
        <el-form-item label="差错账数量" prop="diffCount">
          <el-input v-model="form.diffCount" placeholder="请输入差错账数量" />
        </el-form-item>
        <el-form-item label="是否完成对账, 1:完成，2：未完成， 3：取消" prop="completeState">
          <el-input v-model="form.completeState" placeholder="请输入是否完成对账, 1:完成，2：未完成， 3：取消" />
        </el-form-item>
        <el-form-item label="对账确认时间" prop="confirmDate">
          <el-date-picker clearable
            v-model="form.confirmDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择对账确认时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="账单下载批次" prop="downloadBatch">
          <el-input v-model="form.downloadBatch" placeholder="请输入账单下载批次" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="预留字段1" prop="reserve1">
          <el-input v-model="form.reserve1" placeholder="请输入预留字段1" />
        </el-form-item>
        <el-form-item label="预留字段2" prop="reserve2">
          <el-input v-model="form.reserve2" placeholder="请输入预留字段2" />
        </el-form-item>
        <el-form-item label="预留字段3" prop="reserve3">
          <el-input v-model="form.reserve3" placeholder="请输入预留字段3" />
        </el-form-item>
        <el-form-item label="预留字段4" prop="reserve4">
          <el-input v-model="form.reserve4" placeholder="请输入预留字段4" />
        </el-form-item>
        <el-form-item label="预留字段5" prop="reserve5">
          <el-input v-model="form.reserve5" placeholder="请输入预留字段5" />
        </el-form-item>
        <el-form-item label="删除标识,0:未删除，1：删除" prop="delFlg">
          <el-input v-model="form.delFlg" placeholder="请输入删除标识,0:未删除，1：删除" />
        </el-form-item>
        <el-form-item label="创建用户ID" prop="createUserid">
          <el-input v-model="form.createUserid" placeholder="请输入创建用户ID" />
        </el-form-item>
        <el-form-item label="更新用户ID" prop="updateUserid">
          <el-input v-model="form.updateUserid" placeholder="请输入更新用户ID" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBatch, getBatch, delBatch, addBatch, updateBatch } from "@/api/accounting/batch";

export default {
  name: "Batch",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 对账批次表格数据
      batchList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        billDay: null,
        checkDay: null,
        batchNum: null,
        batchNo: null,
        billType: null,
        orderCount: null,
        checkCount: null,
        diffCount: null,
        completeState: null,
        confirmDate: null,
        createType: null,
        downloadBatch: null,
        remarks: null,
        reserve1: null,
        reserve2: null,
        reserve3: null,
        reserve4: null,
        reserve5: null,
        delFlg: null,
        createUserid: null,
        updateUserid: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        billDay: [
          { required: true, message: "账期年月日不能为空", trigger: "blur" }
        ],
        checkDay: [
          { required: true, message: "对账年月日(yyyymmdd)不能为空", trigger: "blur" }
        ],
        batchNum: [
          { required: true, message: "同一天内的批次序号(对账年月日内)不能为空", trigger: "blur" }
        ],
        batchNo: [
          { required: true, message: "批次号不能为空", trigger: "blur" }
        ],
        createUserid: [
          { required: true, message: "创建用户ID不能为空", trigger: "blur" }
        ],
        createTime: [
          { required: true, message: "创建时间不能为空", trigger: "blur" }
        ],
        updateUserid: [
          { required: true, message: "更新用户ID不能为空", trigger: "blur" }
        ],
        updateTime: [
          { required: true, message: "更新时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询对账批次列表 */
    getList() {
      this.loading = true;
      listBatch(this.queryParams).then(response => {
        this.batchList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        billDay: null,
        checkDay: null,
        batchNum: null,
        batchNo: null,
        billType: null,
        orderCount: null,
        checkCount: null,
        diffCount: null,
        completeState: null,
        confirmDate: null,
        createType: null,
        downloadBatch: null,
        remarks: null,
        reserve1: null,
        reserve2: null,
        reserve3: null,
        reserve4: null,
        reserve5: null,
        delFlg: null,
        createUserid: null,
        createTime: null,
        updateUserid: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加对账批次";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBatch(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改对账批次";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBatch(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBatch(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除对账批次编号为"' + ids + '"的数据项？').then(function() {
        return delBatch(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('accounting/batch/export', {
        ...this.queryParams
      }, `batch_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
